﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FrameworkLib.Filters;

namespace ControlsLib.Filters
{
    public abstract partial class FilterContainerBase2<T> : UserControl, IFilterContainerBase<T> where T : new()
    {
        public event EventHandler ExecuteFilter;
        public event EventHandler ResetFilter;
        public event EventHandler RefreshFilterData;

        private FilterControl m_filterControl = null;
        private T m_filterBag = default(T);
        private bool m_enableDates = true;

        public bool UseDatesFilter 
        { 
            get { return this.checkBox1.Checked; } 
            set 
            { 
                this.checkBox1.Checked = value;

                ((IFilterBase)m_filterBag).UseDates = value;
            } 
        }

        public bool EnableDatesChooser
        {
            get { return m_enableDates; }
            set 
            { 
                m_enableDates = value;

                this.checkBox1.Enabled = value;
                this.checkBox1.Enabled = value;
                this.label2.Enabled = value;
                this.label3.Enabled = value;
                this.dtpFromDate.Enabled = value;
                this.dtpToDate.Enabled = value;
            }
        }

        public T FilterBag
        {
            get { return m_filterBag; }
        }

        public FilterControl ControlManager 
        {
            get { return m_filterControl; }
            set 
            {
                m_filterControl = value;

                m_filterControl.ResetFilter += (o, e) => { OnResetFilter(EventArgs.Empty); };
                m_filterControl.RefrehData += (o, e) => { OnRefreshtFilterData(EventArgs.Empty); };
            } 
        }

        public DateTime? FromDate
        {
            get { return this.dtpFromDate.Value; }
            set 
            { 
                this.dtpFromDate.Value = value.GetValueOrDefault();
                ((IFilterBase)m_filterBag).PeriodFrom = value;
            }
        }

        public DateTime? ToDate
        {
            get { return this.dtpToDate.Value; }
            set { this.dtpToDate.Value = value.GetValueOrDefault(); ((IFilterBase)m_filterBag).PeriodTo = value;  }
        }

        public object PopupContainerControl
        {
            get 
            {
                //PopupContainter pc = new PopupContainter();
                //pc.Container = this.popupContainerControl1;
                //return pc;


                return this.popupContainerControl1;  
            }
        }

        public object PopupContainerControl2
        {
            get { return this.popupContainerControl1; }
        }

        public FilterContainerBase2()
        {
            InitializeComponent();

            this.btnFilter.Click += new EventHandler(btnFilter_Click);
            this.checkBox1.CheckedChanged += new EventHandler(checkBox1_CheckedChanged);

            this.checkBox1.Checked = false;
            this.dtpFromDate.Value = DateTime.Now;
            this.dtpToDate.Value = DateTime.Now;

            this.Size = new Size(34, 26);

            m_filterBag = new T();

            this.Load += new EventHandler(FilterContainerBase2_Load);
        }

        void FilterContainerBase2_Load(object sender, EventArgs e)
        {
            this.dtpFromDate.Value = DateTime.Now;
            this.dtpToDate.Value = DateTime.Now;
        }

        void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            ManageCheckGroupBox(this.checkBox1, this.groupBox1);
        }

        protected virtual void OnExecuteFilter(EventArgs e)
        {
            EventHandler invoker = ExecuteFilter;

            if (invoker != null) invoker(this, e);

            OnFilterButtonClick();
        }

        protected virtual void OnResetFilter(EventArgs e)
        {
            //this.dtpFromDate.Value = DateTime.Now;
            //this.dtpToDate.Value = DateTime.Now;
            //this.UseDatesFilter = false;

            EventHandler invoker = ResetFilter;

            if (invoker != null) invoker(this, e);
        }

        protected virtual void OnRefreshtFilterData(EventArgs e)
        {
            EventHandler invoker = RefreshFilterData;

            if (invoker != null) invoker(this, e);
        }

        void btnFilter_Click(object sender, EventArgs e)
        {
            OnExecuteFilter(EventArgs.Empty);
        }

        public virtual void OnFilterButtonClick() 
        {
            if(this.popupContainerControl1.OwnerEdit != null)
              this.popupContainerControl1.OwnerEdit.ClosePopup();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            this.dtpFromDate.Value = DateTime.Now;
            this.dtpToDate.Value = DateTime.Now;
        }

        private void ManageCheckGroupBox(CheckBox chk, GroupBox grp)
        {
            // Make sure the CheckBox isn't in the GroupBox.
            // This will only happen the first time.
            if (chk.Parent == grp)
            {
                // Reparent the CheckBox so it's not in the GroupBox.
                grp.Parent.Controls.Add(chk);

                // Adjust the CheckBox's location.
                chk.Location = new Point(
                    chk.Left + grp.Left,
                    chk.Top + grp.Top);

                // Move the CheckBox to the top of the stacking order.
                chk.BringToFront();
            }

            // Enable or disable the GroupBox.
            grp.Enabled = (chk.Checked);

            // Get the appropriate color for the contained controls.
            Color fore_color;
            if (grp.Enabled)
            {
                fore_color = Color.FromKnownColor(KnownColor.ActiveCaptionText);
            }
            else
            {
                fore_color = Color.FromKnownColor(KnownColor.InactiveCaptionText);
            }

            // Color the controls in the GroupBox.
            foreach (Control ctl in grp.Controls)
            {
                ctl.ForeColor = fore_color;
            }
        }

        public virtual void RefreshData() {}
    }
}
